using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lavka.Core;
using Lavka.Models;

namespace Lavka.Controllers
{   
    public class ActionsController : Controller
    {
        private LavkaDbEntities context = new LavkaDbEntities();

        //
        // GET: /Actions/

        public ViewResult Index()
        {
            return View(context.Actions.ToList());
        }

        //
        // GET: /Actions/Details/5

        public ViewResult Details(int id)
        {
            Actions actions = context.Actions.Single(x => x.ActionId == id);
            return View(actions);
        }

        //
        // GET: /Actions/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            ViewData["Products"] = new SelectList(context.Products, "ProductId", "Name");
            return View();
        } 

        //
        // POST: /Actions/Create

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(Actions actions)
        {
            if (ModelState.IsValid)
            {
                context.Actions.AddObject(actions);

                if (Request.Form["ProductId"] == null)
                {
                    if (Request.Form["productReference"] != null)
                    {
                        string reference = Request.Form["productReference"].Split('|')[0].Trim(' ');
                        if (!string.IsNullOrEmpty(reference))
                            actions.ProductId = context.Products.Where(p => p.Reference == reference).Single().ProductId;
                    }
                    else
                    {
                        throw new NullReferenceException("ProductId is null");
                    }
                }
                else
                {
                    actions.ProductId = Convert.ToInt32(Request.Form["ProductId"]);
                }

                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(actions);
        }
        
        //
        // GET: /Actions/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            Actions actions = context.Actions.Single(x => x.ActionId == id);
            ViewBag.FoundProduct = context.Products.Where(p => p.ProductId == actions.ProductId).Single();
            ViewData["Products"] = new SelectList(context.Products, "ProductId", "Reference", actions.ProductId);
            return View(actions);
        }

        //
        // POST: /Actions/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(Actions actions)
        {
            if (ModelState.IsValid)
            {
                actions.ProductId = Convert.ToInt32(Request.Form["ProductId"]);
                context.Actions.Attach(actions);
                context.ObjectStateManager.ChangeObjectState(actions, EntityState.Modified);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(actions);
        }

        //
        // GET: /Actions/Delete/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            Actions actions = context.Actions.Single(x => x.ActionId == id);
            return View(actions);
        }

        //
        // POST: /Actions/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize(Roles = "Administrators")]
        public ActionResult DeleteConfirmed(int id)
        {
            Actions actions = context.Actions.Single(x => x.ActionId == id);
            context.Actions.DeleteObject(actions);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public PartialViewResult GetProductByReference(string rawQuery)
        {
            string reference = rawQuery.Split('|')[0].Trim(' ');
            if (!string.IsNullOrEmpty(reference)) ViewBag.FoundProduct = context.Products.Where(p => p.Reference == reference).Single();
            return PartialView("SingleProduct");
        }
    }
}